<?php

/**
 * Class design to manage
 * It provides an abstraction layer
 *
 * @copyright inovia
  * @package oolime_core   
 */
class XmlXslt {
    protected $xml;
    protected $xsl;
    private $prc;
    private $request;
    
    
    /**
     * Constructor, you have to create it if you want to use XSLT.
     *
     * @todo raise error
     */
    public function __construct() {
    global $request;
    $this->request = &$request;
        $this->xsl = new DOMDocument();
        //$this->errorXSL();
        $this->prc = new XSLTProcessor();
    }
    
    /**
     * Load the XML file refering to the specified path filename, into the structure
     *
     * @param string $filename
     * @todo raise error
     */
    public function loadXML($filename) {
        $this->xml = new DOMDocument();
        $this->xml->load($filename);
    }
    
    /**
     * Load the XML DOMNode into the structure
     *
     * @param DOMNode $domxml
     * @todo raise error
     */
    public function setXML($domXML) {
        $this->xml = $domXML;
    }
    
    /**
     * Link the XSLT Processor with the XSL StyleSheet defined
     *
     * @todo raise error
     */
    private function importStyle() {
     //$p=$this->xsl->createElement("div");
       // $value=$this->xsl->CreateTextNode("hello");
        //$p->appendChild($value);
        if($this->request->exist(Array('debug')) && $this->request->get('debug')){
          $debug=new DOMDocument();
          $debug->load("views/debug.xsl");
          $p2=$debug->getElementsByTagName('div')->item(0);

          $pImp=$this->xsl->importNode($p2,true);
          $body = $this->xsl->getElementsByTagName('body');
          $body->item(0)->appendChild($pImp);
        }
        $this->prc->importStyleSheet($this->xsl);
    }
    
    /**
     * Load the error XSL StyleSheet file if the current one is not available
     * 
     * @todo the error file is not available too
     */                   
    private function errorXSL() {
        if( !( @$this->xsl->load("views/error.xsl") ) ) {
            //TODO
            die("Le fichier error.xsl est manquant");
        }
    }
    
    /**
     * Load the XSL StyleSheet file refering to the specified path filename, into the structure
     *
     * @param string $stylename
     * @todo raise error
     */
    public function loadXSL($stylename) {
        if( !( @$this->xsl->load($stylename) ) ) {
            $this->errorXSL();
        }
        $this->importStyle();
    }
    
    /**
     * Load the XSL DOMNode into the structure
     *
     * @param DOMNode $domXSL
     * @todo raise error
     */
    public function setXSL($domXSL) {
        $this->xsl = $domXSL;
        $this->importStyle();
    }
    
    /**
     * Give the result of the transformation by the XSLT Processor, following the structure
     * 
     * @todo raise error
     */    
    public function getResult() {
             
        $dom = $this->prc->transformToDoc($this->xml);
               
        return $dom->saveXML();
    }
    
    /**
     *
     *just return a dom node that is convenient
     *
     */
                        
    public function transform(){
      return $this->prc->transformToDoc($this->xml);
    }
    
    /**
     * Give the result of the transformation by the XSLT Processor of the XML file, following the XSLT StyleSheet file given
     * 
     * @param string $filename
     * @param string $stylename
     * @todo raise error
     */                         
    public function getResultXmlXsl($filename,$stylename) {
        $this->loadXML($filename);
        $this->loadXSL($stylename);
        return $this->getResult();
    }
    
    /**
     * Destructor
     */         
    public function __destruct() {
        $this->prc = null;
        $this->xsl = null;
        $this->xml = null;
    }
}

/*
$xmlxsl = new XmlXslt();
echo $xmlxsl->traiteXmlXsl("index.xml","index.xsl");
*/
?>
